package net.vkits.controller;

import net.vkits.admin.domain.model.Content;
import net.vkits.admin.domain.model.ContentType;
import net.vkits.admin.domain.repository.ContentRepository;
import net.vkits.common.Utils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * Created by ethan-liu on 2017/1/14.
 */
@Controller
@RequestMapping("/contents")
public class ContentsController {
    private static final Logger logger = LoggerFactory.getLogger(BannersController.class);

    @Resource
    private ContentRepository contentRepository;

    @RequiresPermissions("system:sys_role:list")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView contentsIndex() {
        String username = Utils.getUsername(SecurityUtils.getSubject()).orElse("Null");
        ModelMap modelMap = new ModelMap();
        modelMap.put("treeview", "content");
        modelMap.put("sidebar", "contentsIndex");
        modelMap.put("currentUser", username);
        return new ModelAndView("/contentsIndex", modelMap);
    }

    @RequiresPermissions("system:sys_role:list")
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView contents() {
        String username = Utils.getUsername(SecurityUtils.getSubject()).orElse("Null");
        ModelMap modelMap = new ModelMap();
        modelMap.put("treeview", "content");
        modelMap.put("sidebar", "contents");
        modelMap.put("currentUser", username);
        return new ModelAndView("/contents", modelMap);
    }

    @RequiresPermissions("system:sys_role:list")
    @RequestMapping(value = "/indexList", method = RequestMethod.GET)
    @ResponseBody
    public Page<Content> getIndexContents(@PageableDefault(value = 10, sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable) {
        Page<Content> contents = contentRepository.findByType(pageable, ContentType.Index);
        return contents;
    }

    @RequiresPermissions("system:sys_role:list")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Page<Content> getContents(@PageableDefault(value = 10, sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable) {
        Page<Content> contents = contentRepository.findByTypeNot(pageable, ContentType.Index);
        return contents;
    }

    @RequiresPermissions("system:sys_role:add")
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public String addContents(@RequestBody Content content) {
        String username = Utils.getUsername(SecurityUtils.getSubject()).orElse("Null");
        content.setAuthor(username);
        contentRepository.save(content);
        return "done";
    }

    @RequiresPermissions("system:sys_role:del")
    @RequestMapping(value = "/{contentId}", method = RequestMethod.DELETE)
    @ResponseBody
    public String delRole(@PathVariable Long contentId) {
        contentRepository.delete(contentId);
        return "done";
    }
}
